
package ui;
import controllers.PlanDeComidasController;
import domain.Cliente;
import domain.Cobro;
import domain.Dia;
import domain.PlanDeComidas;
import domain.Turno;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.swing.JOptionPane;


public class PlanDeComidasFrm extends javax.swing.JFrame {

    PlanDeComidas plan;
    Cobro cobro;
    PlanDeComidasController planController;
    Cliente cliente;
    Dia dia;
    
    
    public PlanDeComidasFrm() {
        initComponents();
   //     inicializarControles();
        agruparBotones();
        plan = new PlanDeComidas();
        planController = new PlanDeComidasController();
        cliente = new Cliente();
        
        
        
    }
   
   
    private void obtenerDias()
     {
        //HACER VALIDACION DE QUE SE SELECCIONE EL PLAN (10 DIAS, MENSUAL, OTRO)
        
        List<Calendar> listaFechas = null;
        List<Dia> listaDias = new ArrayList<>();
    
   
        
        if(cboxAlmuerzo.isSelected())
        {
            listaFechas = jPCalendario.getSelectedDates();
            for(int i = 0; i<listaFechas.size(); i++)
            {
                dia = new Dia();
                dia.setFecha(listaFechas.get(i));
                dia.setTurno(Turno.ALMUERZO);
                listaDias.add(i, dia);
                
            }
            
        }
        if(cBoxCena.isSelected())
        {
            listaFechas = jPCalendario.getSelectedDates();
            for(int i = 0; i<listaFechas.size(); i++)
            {
                dia = new Dia();
                dia.setFecha(listaFechas.get(i));
                dia.setTurno(Turno.CENA);
                listaDias.add(i, dia);
                
            }
        }
        plan.setListaDias(listaDias);
//        
     }
    
    private void inicializarControles()
    {
        lblCantidadComidas.setText("0");
        lblPrecioPlato.setText("0.00");
        lblPrecioTotal.setText("0.00");
        lblDiferenciaAPagar.setText("0.00"); 
    }
    
    private void agruparBotones()
    {
        btnGroup.add(rbtnOtroPrecio);
        btnGroup.add(rbtnPrecio10Dias);
        btnGroup.add(rbtnPrecioMensual);
        
    }

    
    private void calcularPrecioTotal()
    {

       double cantidadDias = plan.getListaDias().size();
       plan.setPrecioPorComida(25);
       double precioPorPlato = plan.getPrecioPorComida();
       double total = cantidadDias * precioPorPlato;
       lblPrecioTotal.setText(total+"");
    }
   
   
    
    private void cargarLista()
    {
        for(int i=0; i<plan.getListaDias().size(); i++)
        {
            tabla1.setValueAt(plan.getListaDias().get(i).getFecha().toString(), i, i);
            tabla1.setValueAt(plan.getListaDias().get(i).getTurno().toString(), i, i+1);
        }
    }
    
    private List<Date> convertirFecha(List<Calendar> listaCalendar)
    {   
        List<Date>listaDate = null;
        Date fechaDate;
        for(int i = 0; i<listaCalendar.size();i++)
        {
            fechaDate = listaCalendar.get(i).getTime();
            listaDate.add(i, fechaDate);
            
        }
      
        return listaDate;
        
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGroup = new javax.swing.ButtonGroup();
        jLabel7 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jPCalendario = new com.jpcomponents.JPCalendar();
        cboxAlmuerzo = new javax.swing.JCheckBox();
        cBoxCena = new javax.swing.JCheckBox();
        btnOk = new javax.swing.JButton();
        rbtnPrecioMensual = new javax.swing.JRadioButton();
        rbtnPrecio10Dias = new javax.swing.JRadioButton();
        rbtnOtroPrecio = new javax.swing.JRadioButton();
        txtOtro = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        lblPrecioPlato = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        lblCliente = new javax.swing.JLabel();
        btnGuardar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        lblCantidadComidas = new javax.swing.JLabel();
        lblPrecioTotal = new javax.swing.JLabel();
        lblDiferenciaAPagar = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel7.setFont(new java.awt.Font("Perpetua Titling MT", 0, 14)); // NOI18N
        jLabel7.setText("Comedor Universitario");

        jLabel6.setFont(new java.awt.Font("Perpetua Titling MT", 0, 18)); // NOI18N
        jLabel6.setText("UTN Facultad Regional San Francisco");

        jPCalendario.setMultipleSelectionAlowed(true);

        cboxAlmuerzo.setText("Almuerzo");

        cBoxCena.setText("Cena");

        btnOk.setText("Ok");
        btnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkActionPerformed(evt);
            }
        });

        rbtnPrecioMensual.setText("Mensual");

        rbtnPrecio10Dias.setText("10 Días");

        rbtnOtroPrecio.setText("Otro");

        jLabel1.setText("Precio por plato: ");

        lblPrecioPlato.setText("lblPrecioPlato");

        jLabel3.setText("Cantidad de comidas:");

        jLabel4.setText("Precio total: ");

        jLabel5.setText("Diferencia a pagar: ");

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel9.setText("Cliente: ");

        lblCliente.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblCliente.setText("lblCliente");

        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        lblCantidadComidas.setText("lblCantidadComidas");

        lblPrecioTotal.setText("lblPrecioTotal");

        lblDiferenciaAPagar.setText("lblDiferenciaAPagar");

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        tabla1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null}
            },
            new String [] {
                "Fecha", "Turno"
            }
        ));
        jScrollPane1.setViewportView(tabla1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addGap(104, 104, 104))
                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(155, 155, 155))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblPrecioTotal))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblDiferenciaAPagar))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel9)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(lblCliente))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(lblCantidadComidas)))
                                .addGap(31, 31, 31)
                                .addComponent(jPCalendario, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jButton1)
                                    .addComponent(cboxAlmuerzo)
                                    .addComponent(cBoxCena)
                                    .addComponent(btnOk)))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(rbtnPrecio10Dias)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(rbtnOtroPrecio)
                                .addGap(18, 18, 18)
                                .addComponent(txtOtro, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(rbtnPrecioMensual, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblPrecioPlato))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnGuardar)
                        .addGap(38, 38, 38)
                        .addComponent(btnCancelar)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 348, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(114, 114, 114))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(67, 67, 67)
                        .addComponent(cboxAlmuerzo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cBoxCena)
                        .addGap(18, 18, 18)
                        .addComponent(btnOk)
                        .addGap(38, 38, 38)
                        .addComponent(jButton1))
                    .addComponent(jPCalendario, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(lblPrecioTotal))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(lblDiferenciaAPagar))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnGuardar)
                    .addComponent(btnCancelar))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(lblCliente))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(62, 62, 62)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel1)
                                    .addComponent(lblPrecioPlato)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(102, 102, 102)
                                .addComponent(rbtnPrecioMensual)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(rbtnPrecio10Dias)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(rbtnOtroPrecio)
                                    .addComponent(txtOtro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(lblCantidadComidas))
                        .addGap(102, 102, 102))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkActionPerformed
        obtenerDias();
        calcularPrecioTotal();
    }//GEN-LAST:event_btnOkActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
         cliente.setPlanActual(plan);
        planController.guardarPlan(plan);
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        cargarLista();
    }//GEN-LAST:event_jButton1ActionPerformed

    
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PlanDeComidasFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PlanDeComidasFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PlanDeComidasFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PlanDeComidasFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PlanDeComidasFrm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.ButtonGroup btnGroup;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnOk;
    private javax.swing.JCheckBox cBoxCena;
    private javax.swing.JCheckBox cboxAlmuerzo;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private com.jpcomponents.JPCalendar jPCalendario;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblCantidadComidas;
    private javax.swing.JLabel lblCliente;
    private javax.swing.JLabel lblDiferenciaAPagar;
    private javax.swing.JLabel lblPrecioPlato;
    private javax.swing.JLabel lblPrecioTotal;
    private javax.swing.JRadioButton rbtnOtroPrecio;
    private javax.swing.JRadioButton rbtnPrecio10Dias;
    private javax.swing.JRadioButton rbtnPrecioMensual;
    private javax.swing.JTable tabla1;
    private javax.swing.JTextField txtOtro;
    // End of variables declaration//GEN-END:variables
}
